#include "PlaySelectLayer.h"
#include "../../Level/LevelManager.h"
#include "../../../Objects/Ghost/Play/GhostSelect.h"
#include "../../../GameConfig.h"
#include "../../../Support/GameSupport.h"
PlaySelectLayer::PlaySelectLayer(Size screenSize)
{
	m_screenSize = screenSize;
}
void PlaySelectLayer::initGame(Node* nodeRoot,int correctValue)
{
    int numRow = (int)nodeRoot->getChildrenCount();
    float canvasHeight = m_screenSize.height / (numRow + 1);
    float expectHeight = canvasHeight*0.6f;
    //int numGhost = cocos2d::random(0,(int)nodeRoot->getChildByName("nodeNormal")->getChildrenCount());
    
    int ghostCount = 0;
    for (int i = 0; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++)
        {
            ghostCount++;
        }
    }
    
    int posAnswer = cocos2d::random(0, ghostCount - 1);
    
    for (int i = 0,indexGhost = 1; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++,indexGhost++)
        {
            GhostSelect* ghost = new GhostSelect();
            
            switch (LevelManager::s_mode) {
                case kMode_Easy:
                    
                    if (j == posAnswer)
                    {
                        auto str = cocos2d::String::createWithFormat("%d",
                                                                     correctValue)->getCString();
                        auto nodeName = cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString();
                        Vec2 pos = row->getChildByName(nodeName)->getPosition();
                    
                        ghost->initalize(EGhostColor::kGhostColor_Blue ,pos , 1, atoi(str));
                    }
                    else
                    {
                        ghost->initalize(EGhostColor::kGhostColor_Blue, row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1, atoi(cocos2d::String::createWithFormat("%d", random(0, 15))->getCString()));
                    }
                    break;
                case kMode_Normal:
//                    if (j == posAnswer)
//                    {
//                        const char* str = String::createWithFormat("%d", GamePlayConfig::GetInstance()->m_hiddenNumber)->getCString();
//                        ghost->initGhost("ghost_red.png", cocos2d::String::createWithFormat("%d",
//                                                                                            correctValue)->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
//                        
//                    }
//                    else
//                    {
//                        ghost->initGhost("ghost_red.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
//                    }
                    break;
                case kMode_Hard:
//                    if (j == posAnswer)
//                    {
//                        const char* str = String::createWithFormat("%d", GamePlayConfig::GetInstance()->m_hiddenNumber)->getCString();
//                        ghost->initGhost("ghost_purple.png", cocos2d::String::createWithFormat("%d",
//                                                                                               correctValue)->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
//                    }
//                    else
//                    {
//                        ghost->initGhost("ghost_purple.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
//                    }
                    break;
                default:
                    break;
                    
            }
            nodeRoot->addChild(ghost);
        }
        
    }
    nodeRoot->setVisible(true);
    
}

void PlaySelectLayer::initialize(cocos2d::Node *layerCoco)
{
    //m_nodeRoot = layerCoco;
    this->addChild(layerCoco);
    m_Clound = dynamic_cast<Sprite*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_Cloud))));
    m_Clound->setAnchorPoint(Vec2(0.5f, 0.5f));
    Button* btnSettings = dynamic_cast<Button*>(layerCoco->getChildByName(std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_btnSetting))));
    
    m_loadingBar = dynamic_cast<LoadingBar*>(layerCoco->getChildByName(
                                                                               std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_TimeBarBG)))->getChildByName(
                                                                                                                                                                                         std::string(GameSupport::getTextPlayComponent(EPlayComponent::kPlayComponent_TimeBarFG))));
    
    //auto func = CallFunc::create(std::bind(&PlaySelectLayer::calculateTimePlay, this, m_loadingBar));
    //m_loadingBar->runAction(RepeatForever::create(Sequence::create(DelayTime::create(1.0f), func, NULL)));
    

}
void PlaySelectLayer::calculateTimePlay(Ref* pSender)
{
    
    m_loadingBar->setPercent(m_loadingBar->getPercent() - 1);
    //m_currStar = m_loadingBar->getPercent() / 33;
}